import time
import threading

from utils import (
  log_message
)

# 实时记录执行时间
# 用于结束时
class LogProgress:
  
  def __init__(self, count, total):
    self.LOG_PROGRESS_FLAG = False
    self.start_time = time.time()
    self.count = count
    self.total = total

  def start(self, name):
    if self.LOG_PROGRESS_FLAG:
      return
    if self.count != 0:
      elapsed_time = time.time() - self.start_time
      progress = self.count / self.total
      estimated_total_time = elapsed_time / progress if progress > 0 else 0
      remaining_time = int(estimated_total_time) - elapsed_time
  
      hours, remainder = divmod(remaining_time, 3600)
      minutes, seconds = divmod(remainder, 60)

      log_message(f"存储{name}数据进度：{round(progress * 100, 2)}%，估计剩余时间：{int(hours)}小时 {int(minutes)}分钟 {seconds:.2f}秒")
    threading.Timer(10, self.start, [name]).start()

  def close_thread(self):
    self.LOG_PROGRESS_FLAG = True

  def set_count(self, count):
    self.count = count
  
  def set_total(self, total):
    self.total = total
  
  def get_count(self):
    return self.count
  
  def get_total(self):
    return self.total